#ifndef WIDGET_H
#define WIDGET_H

#include "list.h"
#include "filesys.h"
/*
"*******************************************************"
"*                 COPYRIGHT STATEMENT                 *"
"*           THIS HEADER IS CODED BY ZEPHYRBD!         *"
"*This  header is open - sourced under the MIT License.*"
"*       Copyright 2018-2025 Minecraft ZBD Studio.     *"
"*******************************************************"
*/
void searchWidget(List *head,int mode)
{
	if(head==NULL)
	{
		MessageBox(NULL,"系统中未录入数据！","错误",MB_ICONHAND | MB_OK);
		return;
	}
	system("cls");
	int idx = 0;
	char id[11];
	if(mode==6)
	{
		do
		{
			InputBox(id, 11, "输入工号：");
			if (isValidNumber(id)) break;
			else MessageBox(NULL, "工号输入错误，请输入有效的数字！", "提示", MB_ICONINFORMATION | MB_OK);
		} while (1);
	}
	else 
	{
		do
		{
			InputBox(id, 2, "输入文凭 (以数字表示)：\n1-小学及以下,2-初中,3-高中或中技,4-本科或专科,5-研究生及以上");
			if (isValidNumber(id)&&id[0]>='1'&&id[0]<='5') break;
			else MessageBox(NULL, "文凭输入错误，请输入有效的数字！", "提示", MB_ICONINFORMATION | MB_OK);
		} while (1);
	}
	idx = atoi(id);
	if(mode==6) 
	{
		List *p = searchIDItem(head,idx);
		if(p!=NULL)
		{
			printHead();
			printItem(p);
			MessageBox(NULL, "找到了，信息在CMD窗口中！", "提示", MB_ICONINFORMATION | MB_OK);
		}
		else MessageBox(NULL,"查无此人或是系统中未录入数据！","错误",MB_ICONHAND | MB_OK);
	}
	else searchCollegeItem(head,idx);
}

void insertWidget(List *&head)
{
	int t = 0;
	char tmp[5];
	do
	{
		InputBox(tmp, 5, "请输入你要添加的员工人数：");
		if (isValidNumber(tmp)) break;
		else MessageBox(NULL, "请输入有效的数字！", "提示", MB_ICONINFORMATION | MB_OK);
	} while (1);
	t = atoi(tmp);
	while(t--)
	{
		List input;
		input.age = input.money = 0;
		char id[11];
		char college[3];
		char sex[3],age[3],money[6];
		do
		{
			InputBox(id, 11, "插入流程一\n输入工号：");
			if (isValidNumber(id)) break;
			else MessageBox(NULL, "工号输入错误，请输入有效的数字！", "提示", MB_ICONINFORMATION | MB_OK);
		}while(1);
		InputBox(input.name, MAX_LEN, "插入流程二\n输入名字：");
		do
		{
			InputBox(sex, 2, "插入流程三\n输入性别：\n'0'代表女性，'1'代表男性");
			if(sex[0]-'0'==0||sex[0]-'0'==1) break;
			else MessageBox(NULL, "性别输入错误，请输入有效的数字！","提示",MB_ICONINFORMATION | MB_OK);
		}while(1);
		do
		{
			InputBox(age, 3, "插入流程四\n输入年龄：");
			if (isValidNumber(age)) break;
			else MessageBox(NULL, "年龄输入错误，请输入有效的数字！", "提示",  MB_ICONINFORMATION | MB_OK);
		} while (1);
		do
		{
			InputBox(money, 5, "插入流程五\n输入工资：");
			if (isValidNumber(money)) break;
			else MessageBox(NULL, "工资输入错误，请输入有效的数字！", "提示", MB_ICONINFORMATION | MB_OK);
		} while (1);
		do
		{
			InputBox(college, 2, "插入流程六\n输入文凭 (以数字表示)：\n1-小学及以下,2-初中,3-高中或中技,4-本科或专科,5-研究生及以上");
			if (isValidNumber(college)&&college[0]>='1'&&college[0]<='5') break;
			else MessageBox(NULL, "文凭输入错误，请输入有效的数字！", "提示", MB_ICONINFORMATION | MB_OK);
		} while (1);
		
		InputBox(input.address, MAX_LEN, "插入流程七\n输入住址：");
		do
		{
			InputBox(input.tel, 12, "插入流程八\n输入电话：");
			if (isValidNumber(input.tel)) break;
			else MessageBox(NULL, "电话输入错误，请输入有效的数字！", "提示", MB_ICONINFORMATION | MB_OK);
		} while (1);
		input.id = atoi(id);
		input.sex = sex[0]-'0';
		input.age = atoi(age);
		input.college = atoi(college);
		input.money = atoi(money);
		insertList(input,head);
	}
	writeFile(head);
}

void showAllWidget(List *head)
{
	if(head == NULL)
	{
		MessageBox(NULL,"系统中没有任何人的信息！","警告",MB_ICONWARNING | MB_OK);
		return;
	}
	MessageBox(NULL, "信息在CMD窗口中！！!", "提示", MB_ICONINFORMATION | MB_OK);
	printHead();
	struct List *p = head;
	while (p != NULL) 
	{
		printItem(p);
		p = p->next;
	}
}

void editWidget(List *head)
{
	int idx = 0;
	char id[11];
	do
	{
		InputBox(id, 11, "输入工号：");
		if (isValidNumber(id)) break;
		else MessageBox(NULL, "工号输入错误，请输入有效的数字！", "提示", MB_ICONINFORMATION | MB_OK);
	} while (1);
	idx = atoi(id);
	List *found = searchIDItem(head,idx);
	if(found==NULL) MessageBox(NULL,"系统中查无此人！","错误",MB_ICONHAND | MB_OK);
	else 
	{
		string info = "找到工号为 " + to_string(found->id) + " 的员工名叫 " + string(found->name) + "\n";
		string tmp = info + "需要修改？\n0-放弃修改,1-名字,2-性别,3-年龄,4-工资,5-文凭,6-住址,7-电话";
		char edit[2];
		do
		{
			InputBox(edit, 2, tmp.c_str());
			if (isValidNumber(edit)&&edit[0]>='0'&&edit[0]<='7') break;
			else MessageBox(NULL, "修改项输入错误，请输入有效的数字！", "提示", MB_ICONINFORMATION | MB_OK);
		} while(1);
		int op = edit[0]-'0';
		string project;
		int len = 0;
		switch (op) 
		{
			case 1:
				project = "输入姓名：";
				len = MAX_LEN;
				break;
			case 2:
				project = "输入性别：\n'0'代表女性，'1'代表男性";
				len = 2;
				break;
			case 3:
				project = "输入年龄：";
				len = 3;
				break;
			case 4:
				project = "输入工资：";
				len = 5;
				break;
			case 5:
				project = "输入文凭 (以数字表示)：\n1-小学及以下,2-初中,3-高中或中技,4-本科或专科,5-研究生及以上";
				len = 2;
				break;
			case 6:
				project = "输入住址：";
				len = MAX_LEN;
				break;
			case 7:
				project = "输入电话：";
				len = 12;
				break;
			default:
				return;
		}
		char str[MAX_LEN];
		InputBox(str, len, project.c_str());
		if(op==2||op==5)
		{
			int t = str[0] - '0';
			op==2 ? found->sex = t : found->college = t;
		}
		else if(op==3||op==4)
		{
			int t = atoi(str);
			op==3 ? found->age = t : found->money = t;
		}
		else 
		{
			if(op==1) strcpy(found->name,str);
			else if(op==6) strcpy(found->address,str);
			else strcpy(found->tel,str);
		}
		writeFile(head);
		MessageBox(NULL, "修改完成！", "提示", MB_ICONINFORMATION | MB_OK);
	}
}

void deleteWidget(List *&head)
{
	if(head==NULL)
	{
		MessageBox(NULL,"系统中未录入数据！","错误",MB_ICONHAND | MB_OK);
		return;
	}
	int idx = 0;
	char id[13];
	do
	{
		InputBox(id, 11, "输入工号：");
		if (isValidNumber(id)) break;
		else MessageBox(NULL, "工号输入错误，请输入有效的数字！", "提示", MB_ICONINFORMATION | MB_OK);
	} while (1);
	idx = atoi(id);
	deleteItem(head,idx);
}

void copyRightWidget()
{
	system("cls");
	std::cout << "*******************************************************" << std::endl;
	std::cout << "*                 COPYRIGHT STATEMENT                 *" << std::endl;
	std::cout << "*           THE SOFTWARE IS CODED BY ZEPHYRBD!        *" << std::endl;
	std::cout << "*This program is open - sourced under the MIT License.*" << std::endl;
	std::cout << "*       Copyright 2018-2025 Minecraft ZBD Studio.     *" << std::endl;
	std::cout << "*******************************************************" << std::endl;
	MessageBox(NULL, "THE SOFTWARE IS CODED BY ZEPYHRBD!\nThis program is open - sourced under the MIT License.\nCopyright 2018-2025 Minecraft ZBD Studio.", "COPYRIGHT STATEMENT", MB_ICONINFORMATION | MB_OK);
}
#endif
